﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CTManagerSoln
{
    public partial class FrmUserPhongBan : Form
    {
        DataTable dt;
        private bool isUpdate;
        public FrmUserPhongBan()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.TopMost = true;
        }

        private void FrmUserPhongBan_Load(object sender, EventArgs e)
        {
            loadComboUser();
            LoadComboPhongban();
            LoadGrid();
        }

        private void LoadComboPhongban()
        {
            comboboxPhongban.DataSource = clsPhongban.GetAllPhongban();
            comboboxPhongban.DisplayMember = "Tenphongban";
            comboboxPhongban.ValueMember = "MaPhongban";
        }

        private void loadComboUser()
        {
            DataTable dt = User.GetallUser();
            comboxUser.DataSource = dt;
            comboxUser.DisplayMember = "FullName";
            comboxUser.ValueMember = "UserName";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string username = comboxUser.SelectedValue.ToString();
            string phongban = comboboxPhongban.SelectedValue.ToString();
            string tenphongban = comboboxPhongban.SelectedText;
            bool kiemduyet = chckKiemduyet.Checked;

            DataTable dtuserphongban = ClsUserphongban.GetallUserPhongBan();
            
            if (ClsUserphongban.getUserPhongbanbyID(username,phongban).Rows.Count >= 1 && isUpdate==true)
            {
                DialogResult result = MessageBox.Show("thông tin đã tồn tại.", "Thông báo.", MessageBoxButtons.OK);
                return;
            }
            else
            {
                bool checkKiemduyet = false;
                bool checkUsertontai = false;
                for (int i = 0; i < dtuserphongban.Rows.Count; i++)
                {
                    if ((bool)dtuserphongban.Rows[i]["QuyenKiemDuyet"] && dtuserphongban.Rows[i]["MaPhongBan"].ToString()==phongban && chckKiemduyet.Checked)
                    {
                        //MessageBox.Show("quyền kiểm duyệt đã tồn tại.","thông báo",MessageBoxButtons.OK);
                        //return;
                        checkKiemduyet = true;
                    }
                    if (dtuserphongban.Rows[i]["UserName"].ToString() == username && dtuserphongban.Rows[i]["MaPhongBan"].ToString() == phongban)
                    {
                        checkUsertontai = true;
                    }
                }
                if (checkKiemduyet)
                {
                    MessageBox.Show("quyền kiểm duyệt đã tồn tại.", "thông báo", MessageBoxButtons.OK);
                    return;
                }
                if(checkUsertontai)
                {
                    MessageBox.Show("thông tin đã tồn tại.", "thông báo", MessageBoxButtons.OK);
                    return;
                }
                ClsUserphongban.Isert(username, phongban, kiemduyet);
            }            
            isUpdate = false;
            LoadGrid();
        }

        private void LoadGrid()
        {
            gvUserphongban.DataSource = ClsUserphongban.GetallUserPhongBan();
            
        }

        private void gvUserphongban_DoubleClick(object sender, EventArgs e)
        {
            int RowIndex = gvUserphongban.SelectedCells[0].RowIndex;            
            string maphongban = gvUserphongban["MaPhongBan", RowIndex].FormattedValue.ToString();
            string username = gvUserphongban["UserName", RowIndex].FormattedValue.ToString();
            bool kiemduyet = gvUserphongban["QuyenKiemDuyet", RowIndex].FormattedValue==""?false: Convert.ToBoolean(gvUserphongban["QuyenKiemDuyet", RowIndex].FormattedValue);

            if (!string.IsNullOrEmpty(maphongban) && !string.IsNullOrEmpty(username))
            {
                comboxUser.SelectedValue = username;
                comboboxPhongban.SelectedValue = maphongban;
                chckKiemduyet.Checked = kiemduyet;
                isUpdate = true;
            }
        }
    }
}
